package xyz.fefine;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by feng_ on 2016/9/2.
 * 叶子节点,只有一个token,没有孩子
 */
public class ASTLeaf extends ASTree {

    private static List<ASTree> empty = new ArrayList<>();
    protected Token token;

    ASTLeaf(Token token) {
        this.token = token;
    }



    @Override
    public ASTree child(int i) {
        throw new IndexOutOfBoundsException();
    }

    @Override
    public int numChildren() {
        return 0;
    }

    @Override
    public Iterator<ASTree> children() {
        return empty.iterator();
    }

    @Override
    public String location() {
        return "at line "+token.getLineNumber();
    }

    @Override
    public Object eval(Enviorment enviorment) {
        throw new StoneException("cannot eval :" + toString(),this);
    }

    public Token token(){
        return token;
    }

    @Override
    public String toString() {
        return token.getString();
    }
}
